// pages/huodong/index.js

var Pages = 1;
var Pagesize = 0;
var req = require('../../interfaceImpl.js');
var config = require('../../interfaceConfig.js');
var app = getApp();
var AllList_good = [];
var AllList_film = [];
var filmList = [];
var goodList = [];
var ActivityType = 0;
var CinemaCode = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    imgUrl: config.imgHost,
  },

  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  swiperTab: function (e) {
    var that = this;
    this.setData({ currentTab: e.detail.current });
    ActivityType = e.detail.current;
    console.log(ActivityType)
    if (e.detail.current === 0) {
      if (AllList_film.length <= 0 && AllList_good.length <= 0) {
        this.setData({ nohuodong: true })
      } else {


        this.setData({ AllList_good, AllList_film, winHeight: 346 * (AllList_good.length + AllList_film.length)+90, nohuodong:false})
      }
    }
    else if (e.detail.current == 1 && filmList.length == 0) {
      return req.ActivityGoodsList(ActivityType, Pages, Pagesize, 0, CinemaCode).then(res => {
        // wx.showLoading({
        //   title: '获取活动中',
        // })
        filmList = res.data.FilmData
        for (var i = 0; i < AllList.length; i++) {
          filmList[i].discountInfo = filmList[i].EventDiscountInfo.split("￥")[0]
          filmList[i].discountPrice = filmList[i].EventDiscountInfo.split("￥")[1]
        }
        if (filmList.length <= 0) {
          this.setData({ nohuodong: true })
        } else {
          this.setData({ filmList, winHeight: 346 * filmList.length, nohuodong: false})
        }


        // wx.hideLoading();
      })
    } else if (e.detail.current == 2 && goodList.length == 0)
      return req.ActivityGoodsList(ActivityType, Pages, Pagesize, 0, CinemaCode).then(res => {
        // wx.showLoading({
        //   title: '获取活动中',
        // })
        goodList = res.data.Goodsdata
        for (var i = 0; i < goodList.length; i++) {
          goodList[i].discountInfo = goodList[i].EventDiscountInfo.split("￥")[0]
          goodList[i].discountPrice = goodList[i].EventDiscountInfo.split("￥")[1]
        }
        console.log(goodList)
        if (goodList.length <= 0) {
          this.setData({ nohuodong: true })
        } else {
          this.setData({ goodList, winHeight: 346 * goodList.length, nohuodong: false})
        }

        // wx.hideLoading();
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({ nohuodong:false})
    CinemaCode = app.globalData.cinemapojo.CinemaCode
    console.log(CinemaCode)
    AllList_good = []; //初始化列表
    AllList_film = [];
    Pages = 0;
    console.log(123)
    return req.ActivityGoodsList(ActivityType, Pages, Pagesize, 0, CinemaCode).then(res => {
      wx.showLoading({
        title: '获取活动中',
      })
      AllList_good = res.data.Goodsdata;
      AllList_film = res.data.FilmData;
      for (var i = 0; i < AllList_good.length; i++) {
        AllList_good[i].discountInfo = AllList_good[i].EventDiscountInfo.split("￥")[0]
        AllList_good[i].discountPrice = AllList_good[i].EventDiscountInfo.split("￥")[1]
      }

      for (var i = 0; i < AllList_film.length; i++) {
        AllList_film[i].discountInfo = AllList_film[i].EventDiscountInfo.split("￥")[0]
        AllList_film[i].discountPrice = AllList_film[i].EventDiscountInfo.split("￥")[1]
      }

  

      console.log(AllList_good)
      console.log(AllList_film)
      if (AllList_film.length <= 0 && AllList_good.length <= 0) {
        this.setData({ nohuodong: true })
      } else {
        this.setData({ AllList_good, AllList_film, winHeight: 346 * (AllList_good.length + AllList_film.length)})
      }

      wx.hideLoading();
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // TODO下拉刷新
    console.log("刷新")
    Pages++;
    return req.ActivityGoodsList(ActivityType, Pages, Pagesize, 0, CinemaCode).then(res => {
      flag = true;
      console.log(res)
      goodList.push(res.data.data);
      this.setData({ goodList })

    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  joinNow: function (e) {
    //TODO先判断是否已经登录 
    //TODO 这里 判断
    console.log(e)
    if (e.currentTarget.dataset.tp == 1) {
      console.log("index" + e.currentTarget.dataset.id)
      //跳转到电影详情页面
      wx.navigateTo({
        url: '/pages/huodongmovie/detail?EventId=' + e.currentTarget.dataset.id + '',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
    else if (e.currentTarget.dataset.tp == 2) {
      //TODO 跳转到卖品详情页面
      wx.navigateTo({
        url: '/pages/member/activityDetail?EventId=' + e.currentTarget.dataset.id + '',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }

  }

})